﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using MoneyBook.Assets.Resources;
using MoneyBook.ComponentModel;
using System.ComponentModel;
using MoneyBook.Windows;
using MoneyBook.Reflection;
using MoneyBook.Web.Models;
using MoneyBook.Web.Assets.Resources;

namespace MoneyBook.Views {

    [EnhancePropertyAttributeSources(typeof(Member))]
    public partial class LoginViewModel : ViewModelBase {
        
        [ViewModelProperty]
        [ExcludeEnhancePropertyAttributeTypes(typeof(StringLengthAttribute))]
        private System.String memberName_;

        [ViewModelProperty]
        [ExcludeEnhancePropertyAttributeTypes(typeof(StringLengthAttribute))]
        private System.String password_;

        public LoginViewModel() {
    
            this.PropertyChanged += LoginViewModel_PropertyChanged;

            var auth = (INotifyPropertyChanged)WebContext.Current.Authentication;
            var weakListener = new WeakEventListener<LoginViewModel, PropertyChangedEventArgs>(this,
                (instance, sender, args) => instance.AuthenticationPropertyChanged(sender, args),
                (listener) => auth.PropertyChanged -= listener.OnEvent);
            auth.PropertyChanged += weakListener.OnEvent;
    
        }

        private void LoginViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName == "HasErrors" || e.PropertyName == "HasBindingValidationErrors") {
                this.OnPropertyChanged("CanLogin");
            }
        }

        private void AuthenticationPropertyChanged(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName == "IsBusy") {
                this.OnPropertyChanged("CanLogin");
            }
        }

        [ExcludeValidation]
        public bool CanLogin {
            get {
                return !WebContext.Current.Authentication.IsBusy && !this.HasErrors;
            }
        }

        public void Login() {
            if (!this.CanLogin) {
                return;
            }

            this.ValidateAllProperties();

            if (!this.CanLogin) {
                return;
            }

            WebContext.Current.Authentication.Login(new LoginParameters(this.MemberName, this.Password), op => {
                if (op.LoginSuccess) {
                    this.Navigate(PagesWrapper.Current.Views.MainViewUri);
                } else {
                    if (op.HasError) {
                        op.MarkErrorAsHandled();
                        //TODO : display error
                    } else {
                        this.AddError(String.Format(Strings.LoginFaild, Strings.MemberName, Strings.Password), "MemberName", "Password");
                    }
                }
            }, null);
        }

        protected override EnhancePropertyAttributeType GetEnhancePtopertyAttributeType() {
            return new EnhancePropertyAttributeType(typeof(LoginViewModel));
        }
    }
}
